package com.shxzz.easy12306.services.pay.handler.ali;

import com.shxzz.easy12306.services.pay.common.enums.PayChannelEnum;
import com.shxzz.easy12306.services.pay.common.enums.TradeStatusEnum;
import com.shxzz.easy12306.services.pay.dto.ali.AliPayCallbackRequest;
import com.shxzz.easy12306.services.pay.dto.base.PayCallbackRequest;
import com.shxzz.easy12306.services.pay.dto.domain.PayCallbackDTO;
import com.shxzz.easy12306.services.pay.handler.base.PayCallbackHandler;
import com.shxzz.easy12306.services.pay.service.PayService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 阿里支付请求回调处理器
 * @author shxzz
 * @create 2024-03-11-23:43
 */
@RequiredArgsConstructor
@Component
public class AliPayCallbackHandler implements PayCallbackHandler {

    private final PayService payService;

    @Override
    public void execute(PayCallbackRequest requestParam) {
        callback(requestParam);
    }

    @Override
    public void callback(PayCallbackRequest payCallbackRequest) {
        AliPayCallbackRequest aliPayCallbackRequest = (AliPayCallbackRequest) payCallbackRequest;
        PayCallbackDTO payCallbackRequestParam = PayCallbackDTO.builder()
                .status(TradeStatusEnum.findTradeCode(aliPayCallbackRequest.getTradeStatus()))
                .payAmount(aliPayCallbackRequest.getBuyerPayAmount().intValue())
                .tradeNo(aliPayCallbackRequest.getTradeNo())
                .gmtPayment(aliPayCallbackRequest.getGmtPayment())
                .orderSn(aliPayCallbackRequest.getOutTradeNo())
                .channel(PayChannelEnum.ALI_PAY.getName())
                .build();
        payService.callbackPay(payCallbackRequestParam);
    }

    @Override
    public String mark() {
        return PayChannelEnum.ALI_PAY.getName();
    }
}
